package com.lktx.sso.admin.service;

import cn.hserver.core.ioc.annotation.Autowired;
import cn.hserver.core.ioc.annotation.Bean;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.lktx.sso.admin.entity.SsoOrganization;
import com.lktx.sso.admin.mapper.SsoOrganizationMapper;
import com.lktx.sso.admin.vo.OrganizationListTreeVO;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * sso组织管理 服务类
 *
 */
@Slf4j
@Bean
public class SsoOrganizationService {

    @Autowired
    private SsoOrganizationMapper mapper;

    /**
     * 分页查询列表
     *
     * @param page   分页对象
     * @param entity 查询实体
     * @return 所有数据
     */
    public Page<SsoOrganization> page(Page<SsoOrganization> page, SsoOrganization entity) {
        return mapper.paginate(page,new QueryWrapper()
                    .like(SsoOrganization::getName, entity.getName(), StrUtil.isNotEmpty(entity.getName()))
                    .like(SsoOrganization::getRemark, entity.getRemark(), StrUtil.isNotEmpty(entity.getRemark()))
                .orderBy(SsoOrganization::getSsoOrganizationId,false)
        );
    }

    /**
     * 不分页查询列表
     *
     * @param entity 实体对象
     * @return 查询结果
     */
    public List<SsoOrganization> list(SsoOrganization entity) {
        return mapper.selectListByQuery(new QueryWrapper()
                    .like(SsoOrganization::getName, entity.getName(), StrUtil.isNotEmpty(entity.getName()))
                    .like(SsoOrganization::getRemark, entity.getRemark(), StrUtil.isNotEmpty(entity.getRemark()))
                .orderBy(SsoOrganization::getSsoOrganizationId,false)
        );
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    public SsoOrganization getById(Integer id) {
        return mapper.selectOneById(id);
    }


    /**
     * 新增单条数据
     *
     * @param entity 实体对象
     */
    public void save(SsoOrganization entity) {
        if (entity.getSsoOrganizationPid() == null) {
            entity.setSsoOrganizationPid(0);
        }
        mapper.insert(entity);
    }

    /**
     * 修改单条数据
     *
     * @param entity 实体对象
     */
    public void update(SsoOrganization entity) {
        mapper.update(entity);
    }

    /**
     * 批量删除数据
     *
     * @param ids 主键集合
     */
    public void deleteByIds(List<Integer> ids) {
        mapper.deleteBatchByIds(ids);
    }


    /**
     * 获取权限列表和树结构
     */
    public OrganizationListTreeVO getListAndTree() {
        OrganizationListTreeVO organizationListTreeDTO = new OrganizationListTreeVO();
        List<SsoOrganization> list = list(new SsoOrganization());
        organizationListTreeDTO.setList(list);
        organizationListTreeDTO.setTree(buildTree(list));
        return organizationListTreeDTO;
    }

    private List<SsoOrganization> buildTree(List<SsoOrganization> list) {
        if (CollUtil.isEmpty(list)) return Collections.emptyList();
        Map<Integer, List<SsoOrganization>> parentIdMap = list.stream().collect(Collectors.groupingBy(SsoOrganization::getSsoOrganizationPid));
        Integer parentId = Collections.min(parentIdMap.keySet());
        List<SsoOrganization> menus = parentIdMap.get(parentId);
        return fillChildren(menus, parentIdMap);
    }

    private List<SsoOrganization> fillChildren(List<SsoOrganization> list, Map<Integer, List<SsoOrganization>> parentIdMap) {
        return list.stream().peek(i -> {
            if (parentIdMap.containsKey(i.getSsoOrganizationId())) {
                i.setChildren(fillChildren(parentIdMap.get(i.getSsoOrganizationId()), parentIdMap));
            }
        }).collect(Collectors.toList());
    }

}